package com.lanou3g.ptp.api.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageInfo;
import com.lanou3g.ptp.api.service.ApiStatisticalService;
import com.lanou3g.ptp.attendance.service.AttendanceService;
import com.lanou3g.ptp.entity.*;
import com.lanou3g.ptp.service.ApprovalService;
import com.lanou3g.ptp.student.service.StudentService;
import com.lanou3g.ptp.user.service.UserService;
import com.lanou3g.utils.PtpResult;
import com.lanou3g.utils.PtpResultUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class ApiStatisticalServiceImpl implements ApiStatisticalService {
    @Reference
    private AttendanceService attendanceService;
    @Reference
    private ApprovalService approvalService;
    @Reference
    private StudentService studentService;
    @Reference
    private UserService userService;

    @Override
    public StatisticalAll ratio(Date startDate, Date endDate, Long taskId, Integer page, Integer size, HttpServletResponse response) throws ParseException, IOException {
        NumberFormat n = NumberFormat.getPercentInstance();
        n.setMaximumIntegerDigits(3);
        n.setMaximumFractionDigits(2);
        StatisticalAll statisticalAll = new StatisticalAll();
        List<String> studentName = new ArrayList<>();
        PageInfo<Student> studentPageInfo = studentService.findAll(page, size);
        List<Student> students = studentPageInfo.getList();
        List<String> formats = new ArrayList<>();
        for (Student student : students) {
            Long studentId = student.getId();
            User user = userService.userById(studentId);
            studentName.add(user.getName());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
            String date1 = sdf.format(startDate);
            String date2 = sdf.format(endDate);
            Date d1 = sdf.parse(date1);
            Date d2 = sdf.parse(date2);
            Long integerAll = (d2.getTime() - d1.getTime() + 1000000) / (60 * 60 * 24 * 1000);
            Integer num = 0;
            List<List<Date>> dates = attendanceService.findByAttentTime(studentId, taskId, startDate, endDate);
            for (List<Date> date : dates) {
                if (date.size() == 1) {
                    if (date.get(0) == null) {
                        num = num + 0;
                    } else {
                        num = num + 1;
                    }
                } else if (date.size() >= 2) {
                    num = num + 2;
                } else {
                    num = num + 0;
                }
            }
            Double d = Double.valueOf(num / ((integerAll + 1L) * 2D));
            String format = n.format(d);
            formats.add(format);
            statisticalAll.setFormat(formats);
            statisticalAll.setStudentName(studentName);
        }
        return statisticalAll;
    }

    @Override
    public PtpResult ratioAll(Date startTime, Integer page, Integer size, Long taskId) {
        Statistical statistical = new Statistical();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);
        Date endTime = startTime;
        List<Date> MDate = new ArrayList<>();
        List<Date> EDate = new ArrayList<>();
        PageInfo<Student> students = studentService.findAll(page, size);
        List<Student> studentsList = students.getList();
        List<String> studentNameList = new ArrayList<>();
//        循环出所有的学生
        for (Student student : studentsList) {
            Long studentId = student.getId();
            User user = userService.userById(studentId);
            studentNameList.add(user.getName());
//            根据学生id和起始日期查出在时间段内所有打卡的学生
            List<List<Date>> attandances = attendanceService.findByAttentTime(studentId, taskId, startTime, endTime);
            System.out.println(attandances.size() + "pppppp");
//            遍历成每个人在一个时间段内打卡的数组
            for (List<Date> attandance : attandances) {
//                判断数组长度如果数组长度大于二的话第一个元素是早上打卡的时间 最后一个元素是晚上打卡的时间
                if (attandance.size() >= 2) {
                    MDate.add(attandance.get(0));
                    EDate.add(attandance.get(attandance.size() - 1));
                } else if (attandance.size() == 1) {
                    if (attandance.get(0) != null) {
                        SimpleDateFormat format = new SimpleDateFormat("HH");
                        String hour = format.format(attandance.get(0));
                        int hour1 = Integer.parseInt(hour);
                        if (hour1 >= 12) {
                            EDate.add(attandance.get(0));
                            MDate.add(null);
                            System.out.println(MDate.size() + "");
                        } else {
                            MDate.add(attandance.get(0));
                            EDate.add(null);
                        }
                    } else {
                        EDate.add(null);
                        MDate.add(null);
                    }
                } else {
                    MDate.add(null);
                    EDate.add(null);
                }
            }
        }
        statistical.setStudentNameList(studentNameList);
        statistical.setMDate(MDate);
        statistical.setEDate(EDate);

        System.out.println(MDate);
        System.out.println(EDate.size() + "/////");
        return PtpResultUtils.ok(statistical);
    }


    public static List<Date> findDates(Date dBegin, Date dEnd) {
        List lDate = new ArrayList();
        lDate.add(dBegin);
        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calBegin.setTime(dBegin);
        Calendar calEnd = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calEnd.setTime(dEnd);
        // 测试此日期是否在指定日期之后
        while (dEnd.after(calBegin.getTime())) {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            lDate.add(calBegin.getTime());
        }
        return lDate;
    }
}
